package com.mmy.service.train.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.UtilConstants;
import com.mmy.dao.train.TrainPlanRepository;
import com.mmy.dao.train.VTrainPlanRepository;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.train.TTrainPlan;
import com.mmy.domain.train.VTrainPlan;
import com.mmy.service.ServiceCommon;
import com.mmy.service.train.TrainPlanService;

@Service
public class TrainPlanServiceImpl implements TrainPlanService {

	@Autowired
	TrainPlanRepository trainPlanRepository;
	@Autowired
	VTrainPlanRepository vTrainPlanRepository;
	@Autowired
	ServiceCommon common;

	/**
	 * 查找全部
	 * 
	 * @param
	 * @return
	 */
	public List<VTrainPlan> findAll() {
		// TODO Auto-generated method stub
		return vTrainPlanRepository.findAll();
	}

	/**
	 * 通过计划单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public VTrainPlan findByID(int iPlanID) {
		// TODO Auto-generated method stub
		return vTrainPlanRepository.findByID(iPlanID);
	}

	/**
	 * 获取计划单最大编码
	 * 
	 * @return 返回值说明 计划单编码
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.16
	 */
	public String getMaxPlanID() {
		// 返回计划单最大编码
		Object objCode = trainPlanRepository.getMaxPlanID();
		String strPlanID = "";
		if (objCode != null) {
			strPlanID = objCode.toString();
		}
		return strPlanID;
	}

	/**
	 * 新增id
	 * 
	 * @param
	 * @return
	 */
	public void save(int planID) {
		// TODO Auto-generated method stub
		trainPlanRepository.save(planID);
	}

	/**
	 * 新增
	 * 
	 * @param
	 * @return
	 */
	public void save(TTrainPlan tTrainPlan) {
		// TODO Auto-generated method stub
		trainPlanRepository.save(tTrainPlan);
	}

	/**
	 * 删除
	 * 
	 * @param
	 * @return
	 */
	public void delTrainPlan(List<Integer> lstPlanID) {
		// TODO Auto-generated method stub
		trainPlanRepository.delTrainPlan(lstPlanID);
	}

	/**
	 * 通过id查找
	 * 
	 * @param
	 * @return
	 */
	public TTrainPlan findByPlanID(int iPlanID) {
		// TODO Auto-generated method stub
		return trainPlanRepository.findByPlanID(iPlanID);
	}

	/**
	 * 通过name查找
	 * 
	 * @param
	 * @return
	 */
	public List<VTrainPlan> findByName(String strTrainName) {
		// TODO Auto-generated method stub
		return vTrainPlanRepository.findByName(strTrainName);
	}

	/**
	 * 通过ID查找
	 * 
	 * @param
	 * @return
	 */

	public List<VTrainPlan> findByTrainID(int iTrainID) {
		// TODO Auto-generated method stub
		return vTrainPlanRepository.findByTrainID(iTrainID);
	}

	/**
	 * 函数功能说明： 跳转进入前台火车票页面 火车票推荐模块 推荐由北京出发的机票信息 国内
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.02.24 修改时间2021.03.17
	 */
	public List<VTrainPlan> findByZoneOutName(String strZoneOutName) {
		// TODO Auto-generated method stub
		return vTrainPlanRepository.findByZoneOutName(strZoneOutName);
	}

	/**
	 * 函数功能说明： 跳转进入前台飞机机票页面 机票推荐模块 推荐由北京出发的机票信息 国外
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人Hanxl
	 * @date 创建日期2021.02.24 修改时间2021.03.17
	 */

	public List<VTrainPlan> findOutByZoneOutName() {
		return vTrainPlanRepository.findOutByZoneOutName();
	}

	/**
	 * 函数功能说明： 查询列车时刻 单程 往返一样
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间 2021.03.17
	 */
	public List<VTrainPlan> findSingle(String strOutCityName, String strInCityName, String strOutDate) {
		Date outDate = new Date();
		// 判断是否为空
		if (strOutCityName == null || strOutCityName == "") {
			strOutCityName = "北京市";
		}
		if (strInCityName == null) {
			strInCityName = "";
		}
		if (!(strOutDate == null || strOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();

		} else {

		}

		return vTrainPlanRepository.findSingle(strOutCityName, strInCityName, outDate);
	}

	/**
	 * 后台列车时刻管理中显示自己公司的时刻信息如果为管理员显示全部
	 *
	 * @author 创建人 hanxl
	 * @param strPlaneID
	 * @date 创建日期 2021.02.22
	 */
	public List<VTrainPlan> findTrainPlan(TSysUser sessionSysUser, String strTrainID) {
		int iOrgID = sessionSysUser.getOrgID();
		int iTrainID = Integer.parseInt(strTrainID);
		List<VTrainPlan> lstTrainPlan = new ArrayList<VTrainPlan>();
		if (sessionSysUser.getUserID() == 0) {
			lstTrainPlan = vTrainPlanRepository.findAll(iTrainID);
		} else {
			lstTrainPlan = vTrainPlanRepository.findByOrgID(iOrgID, iTrainID);
		}
		return lstTrainPlan;
	}

	/**
	 * 后台航班信息管理中显示自己公司的航班信息如果为管理员显示全部 异步加载表格
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @param strPlaneID
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public List<VTrainPlan> findByOrgID(String strTrainID, TSysUser sessionSysUser, String strTrainOutDate,
			String strTrainOutZone, String strTrainInZone, int startIndex, int count) {
		int iTrainID = Integer.parseInt(strTrainID);
		if (strTrainOutZone == null)
			strTrainOutZone = "";
		if (strTrainInZone == null)
			strTrainInZone = "";
		int iOrgID = sessionSysUser.getOrgID();
		List<VTrainPlan> lstTrainPlan = new ArrayList<VTrainPlan>();
		Date outDate = new Date();
		if (!(strTrainOutDate == null || strTrainOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strTrainOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();
			if (sessionSysUser.getUserID() == 0) {
				lstTrainPlan = vTrainPlanRepository.findSearchALLDate(iTrainID, outDate, strTrainOutZone,
						strTrainInZone, startIndex, count);
			} else {
				lstTrainPlan = vTrainPlanRepository.findSearchByOrgIDDate(iTrainID, iOrgID, outDate, strTrainOutZone,
						strTrainInZone, startIndex, count);
			}
		} else {
			if (sessionSysUser.getUserID() == 0) {
				lstTrainPlan = vTrainPlanRepository.findSearchALL(iTrainID, strTrainOutZone, strTrainInZone, startIndex,
						count);
			} else {
				lstTrainPlan = vTrainPlanRepository.findSearchByOrgID(iTrainID, iOrgID, strTrainOutZone, strTrainInZone,
						startIndex, count);
			}
		}
		return lstTrainPlan;
	}

	/**
	 * 后台列车时刻信息管理中显示自己公司的列车时刻信息如果为管理员显示全部 分页
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @param strPlaneID
	 * @date 创建日期 2020.11.19 修改时间2021.02.22
	 */
	public int queryPlanCount(String strTrainID, TSysUser sessionSysUser, String strTrainOutDate,
			String strTrainOutZone, String strTrainInZone) {
		int iTrainID = Integer.parseInt(strTrainID);
		Date outDate = new Date();
		if (strTrainOutZone == null)
			strTrainOutZone = "";
		if (strTrainInZone == null)
			strTrainInZone = "";
		int iOrgID = sessionSysUser.getOrgID();
		int count = 0;
		if (!(strTrainOutDate == null || strTrainOutDate == "")) {
			// 处理date
			Calendar calStartDate = Calendar.getInstance();
			calStartDate.setTime(common.formatDate(strTrainOutDate, UtilConstants.DateStyle.YYYY_MM_DD));
			calStartDate.set(Calendar.HOUR_OF_DAY, 0);
			calStartDate.set(Calendar.MINUTE, 0);
			calStartDate.set(Calendar.SECOND, 0);
			calStartDate.set(Calendar.MILLISECOND, 1);
			outDate = calStartDate.getTime();
			if (sessionSysUser.getUserID() == 0) {
				count = vTrainPlanRepository.countALLDate(iTrainID, outDate, strTrainOutZone, strTrainInZone);
			} else {
				count = vTrainPlanRepository.countByOrgIDDate(iTrainID, iOrgID, outDate, strTrainOutZone,
						strTrainInZone);
			}
		} else {
			if (sessionSysUser.getUserID() == 0) {
				count = vTrainPlanRepository.countALL(iTrainID, strTrainOutZone, strTrainInZone);
			} else {
				count = vTrainPlanRepository.countByOrgID(iTrainID, iOrgID, strTrainOutZone, strTrainInZone);
			}
		}

		return count;
	}

}
